/**
 * Wihidum - Efficient Broker Hierarchy for Publish Subscribe Middleware
 *
 * License      :   Apache License 2.0
 * Home Page    :   http://code.google.com/p/wihidum/
 * Mailing List :   wihidum@googlegroups.com
 */

package lk.mrt.cse.wihidum.publisher.client;

import org.apache.axiom.om.*;
import org.apache.axis2.*;
import org.apache.axis2.addressing.*;
import org.apache.axis2.client.*;

import javax.xml.namespace.*;

/**
 * Client invoking publisher service
 */
public class Client
{
    private UserInterface userInterface;

    public Client (UserInterface userInterface)
    {
        this.userInterface = userInterface;
    }

    /**
     * Invoking publish method in publisher service
     *
     * @param brokerAddress    Address of the broker
     * @param publisherAddress Address of the publisher
     * @param topic            Topic of the publication
     * @param publicationText  Data of the publication
     */
    public void publish (String brokerAddress, String adminAddress, String publisherAddress, String topic, String publicationText)
    {
        try
        {
            ServiceClient serviceClient = new ServiceClient ();
            Options options = new Options ();
            options.setTo (new EndpointReference (publisherAddress));
            options.setAction ("publish");
            serviceClient.setOptions (options);

            OMFactory factory = OMAbstractFactory.getOMFactory ();

            OMElement messageElement = factory.createOMElement ("PublicationRequest", null);

            OMElement publicationHeader = factory.createOMElement ("Header", null);
            OMElement actionElement = factory.createOMElement ("Action", null);
            actionElement.setText ("publish");
            publicationHeader.addChild (actionElement);
            messageElement.addChild (publicationHeader);

            OMElement brokerAddressElement = factory.createOMElement ("brokerAddress", null);
            brokerAddressElement.setText (brokerAddress);
            messageElement.addChild (brokerAddressElement);

            OMElement adminAddressElement = factory.createOMElement("adminAddress", null);
            adminAddressElement.setText(adminAddress);
            messageElement.addChild(adminAddressElement);


            OMElement publisherAddressElement = factory.createOMElement ("publisherAddress", null);
            publisherAddressElement.setText (publisherAddress);
            messageElement.addChild (publisherAddressElement);

            OMElement topicElement = factory.createOMElement ("topic", null);
            topicElement.setText (topic);
            messageElement.addChild (topicElement);

            OMElement publicationTextElement = factory.createOMElement ("publicationText", null);
            publicationTextElement.setText (publicationText);
            messageElement.addChild (publicationTextElement);

            OMElement responseElement = serviceClient.sendReceive (messageElement);

            OMElement referenceParameters = responseElement.getFirstChildWithName (new QName ("ReferenceParameters"));
            OMElement responseTextElement = referenceParameters.getFirstChildWithName (new QName ("Response"));

            userInterface.showInfo (responseTextElement.getText ());
        }
        catch (NullPointerException ex)
        {
            userInterface.showError ("Error in processing publication response");
        }
        catch (AxisFault ex)
        {
            userInterface.showError ("Error in sending publication request");
        }
    }

}
